package servlet;
import bean.Hero;
import service.HeroService;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/HeroAddServlet")
public class HeroAddServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /**request.setCharacterEncoding("UTF-8");*/


        try {

            //1. 获取客户端提交上来的数据
            String name = request.getParameter("name");
            float hp = Float.parseFloat(request.getParameter("hp"));
            int damage = Integer.parseInt(request.getParameter("damage"));

            //2. 添加到数据库
            Hero hero = new Hero(name, hp, damage);
            HeroService service = new HeroService();
            service.add(hero);

            //3. 跳转到列表页
            //再查一次数据库，然后再装到作用域中 ，然后再跳转。
            //这里是直接跳转到页面上， 那么这个页面会重新翻译一次，上面的那个request的请求存放的数据是没有了。
            //request.getRequestDispatcher("list.jsp").forward(request, response);

            //servlet除了能跳jsp之外。 还能跳servlet
            request.getRequestDispatcher("addHero.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
        }




    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

